package com.longyi.admin.controller;

import com.longyi.admin.service.LogService;
import com.longyi.admin.entity.Log;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import com.longyi.admin.common.QueryPageParam;
import com.longyi.admin.common.Result;

/**
* Log 前端控制器
*
* @author 龙毅
* @since 2025-09-08
*/
@RestController
@RequestMapping("/log")
public class LogController {

    // 注入service
    @Autowired
    private LogService logService;

    // 统计数量
    @GetMapping("/count")
    public Integer count() {
        return logService.count();
    }

    // 获取列表
    @GetMapping("/list")
    public List<Log> list() {
        return logService.list();
    }

    // 新增数据
    @PostMapping("/save")
    public Result save(@RequestBody Log log) {
        return logService.save(log) 
            ? Result.suc("新增成功") 
            : Result.fail("新增失败");
    }

    // 修改数据
    @PostMapping("/update")
    public Result update(@RequestBody Log log) {
        return logService.updateById(log)
            ? Result.suc("修改成功") 
            : Result.fail("修改失败");
    }

    // 获取详情
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        Log log = logService.getById(id);
        return Result.suc(log);
    }

    // 批量删除数据
    @Transactional
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody Long[] ids) {
        logService.removeByIds(Arrays.asList(ids));
        return Result.suc("批量删除成功");
    }

    // 分页查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();

        // 提取查询参数
            String id = (String) param.get("id");
            String user = (String) param.get("user");
            String record = (String) param.get("record");
            String type = (String) param.get("type");
            String createTime = (String) param.get("createTime");

        // 分页配置
        Page<Log> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        // 构建查询条件
        LambdaQueryWrapper<Log> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                if (StringUtils.isNotBlank(id) && !"null".equals(id)) {
                    lambdaQueryWrapper.eq(Log::getId, id);
                }
                if (StringUtils.isNotBlank(user) && !"null".equals(user)) {
                    lambdaQueryWrapper.like(Log::getUser, user);
                }
                if (StringUtils.isNotBlank(record) && !"null".equals(record)) {
                    lambdaQueryWrapper.like(Log::getRecord, record);
                }
                if (StringUtils.isNotBlank(type) && !"null".equals(type)) {
                    lambdaQueryWrapper.like(Log::getType, type);
                }
                if (StringUtils.isNotBlank(createTime) && !"null".equals(createTime)) {
                    lambdaQueryWrapper.like(Log::getCreateTime, createTime);
                }

        // 执行分页查询
        IPage result = logService.PageList(page, lambdaQueryWrapper);
        return Result.suc(result.getTotal(), result.getRecords());
    }
}